package com.ztstkj.lzboard.services

import android.app.Service
import android.content.Intent
import android.os.IBinder
import android.util.Log
import com.sscl.baselibrary.utils.DebugUtil
import com.ztstkj.lzboard.ui.MainActivity
import com.ztstkj.lzservices.utils.ServiceAliveUtils
import java.util.Timer
import java.util.TimerTask

class BootService: Service() {
    private  var  timer:Timer?=null
    private  val tag: String=this.javaClass.name
    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        // 开启一个定时器，每隔一段时间检查设备状态
        timer =  Timer()
        timer?.schedule(object :TimerTask() {
            override fun run() {
                var isBootCompleted = isBootCompleted()// 检查设备状态
                if (isBootCompleted) {
                    DebugUtil.debugOut(tag,"启动应用")
                    // 启动我们的应用
                    var launchIntent =  Intent(applicationContext,MainActivity::class.java)
                    launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                    startActivity(launchIntent)
                }
            }
        }, 1000, 1000*60); // 每隔1秒检查一次设备状态
        return super.onStartCommand(intent, flags, startId)
    }
    override fun onBind(intent: Intent?): IBinder? {
        return null
    }

    override fun onDestroy() {
        // 取消定时器
        if (timer != null) {
            timer?.cancel()
            timer = null
        }
        super.onDestroy()
    }
    fun isBootCompleted():Boolean{
        // 检查设备状态，例如通过读取系统属性或查询系统服务
        return ServiceAliveUtils.isServiceAlice(this,"com.ztstkj.lzboard.services.BootService") // 假设设备状态正常
    }
}